<template>
  <!-- page:operationduty -->
  <div style="margin-top: 15px;">
    <!-- # 条件搜索 -->
    <el-row style="background-color: #FFFFFF;">
      <el-col :span="10">
        <el-form ref="searchForm" label-width="80px" :rules="searchRules" :model="searchData" style="display:flex;margin-top:8px ">
          <el-form-item label="值班姓名">
            <el-input v-model="searchData.dutyPerson" />
          </el-form-item>
          <el-form-item label="上班情况">
            <el-select v-model="searchData.shift" placeholder="上班情况">
              <el-option label="上班" value="0" />
              <el-option label="下班" value="1" />
            </el-select>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="2" style="margin-top:8px">
        <el-button type="primary" plain @click="handleSearch">搜索</el-button>
      </el-col>
      <el-col :span="3" style="margin-top:8px">
        <el-button type="primary" icon="el-icon-refresh" circle @click="handleRefresh" />
      </el-col>
      <el-col :span="3" style="margin-top:8px">
        <el-button type="primary" plain @click="addDuty">添加值班</el-button>
      </el-col>
    </el-row>

    <!-- # 表格数据 -->
    <el-table v-loading="loading" element-loading-text="正在加载中" :data="tableData" style="width: 99%" stripe>

      <el-table-column label="序号" width="80" type="index" align="center" />

      <el-table-column label="姓名" width="180" align="center">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p>姓名: {{ scope.row.dutyPerson }}</p>
            <p>性别: {{ scope.row.address }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.dutyPerson }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>

      <el-table-column label="值班日期" width="250">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span style="margin-left: 10px">{{ formatDate(scope.row.dutyDate) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="上班情况" width="140">
        <template slot-scope="scope">
          <span v-if="scope.row.shift == '0'">上班</span>
          <span v-if="scope.row.shift == '1'">下班</span>
        </template>
      </el-table-column>

      <el-table-column label="在岗情况" width="140">
        <template slot-scope="scope">
          <span v-if="scope.row.currentStatus == '0'">在岗</span>
          <span v-if="scope.row.currentStatus == '1'">未在岗</span>
        </template>
      </el-table-column>

      <el-table-column label="备注信息" width="250" prop="remarks" />

      <el-table-column label="更新日期" width="250">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span style="margin-left: 10px">{{ formatDate(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="创建日期" width="250">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span style="margin-left: 10px">{{ formatDate(scope.row.createTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" min-width="150" fixed="right">
        <template slot-scope="scope">
          <el-button
            v-permission="'duty:modify'"
            size="mini"
            type="info"
            @click="handleEdit(scope.row)">
            编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
      <template v-slot:empty>
        <div style="height: 200px;">
          <img src="../../assets/nodata.png" width="150" style="margin: 30px;">
        </div>
      </template>
    </el-table>
    <!-- # 分页 -->
    <el-pagination
      :page-size="size"
      :current-page.sync="page"
      layout="sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- # 弹层 -->
    <el-dialog :title="isEdit ? '编辑' : '添加'" :visible.sync="dialogVisible" width="50%">
      <div>
        <el-form ref="formDateRef" :rules="rules" label-position="right" label-width="120px" :model="formDate">

          <el-form-item label="值班人" prop="dutyPerson">
            <el-input v-model="formDate.dutyPerson" placeholder="填写值班人姓名" />
          </el-form-item>

          <el-form-item label="值班日期" prop="dutyDate">
            <el-date-picker v-model="formDate.dutyDate" type="datetime" placeholder="选择日期时间" />
          </el-form-item>

          <el-form-item label="上班情况" prop="shift">
            <el-radio-group v-model="formDate.shift">
              <el-radio :label="0">上班</el-radio>
              <el-radio :label="1">下班</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="在岗情况" prop="currentStatus">
            <el-radio-group v-model="formDate.currentStatus">
              <el-radio :label="0">在岗</el-radio>
              <el-radio :label="1">不在岗</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="备注信息">
            <el-input v-model="formDate.remarks" placeholder="填写备注信息" />
          </el-form-item>

        </el-form>

      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelBtn">取 消</el-button>
        <el-button type="primary" @click="verBtn">确 定</el-button>
      </span>
    </el-dialog>

  </div>

</template>

<script>
import { getDuty, delDuty, addDuty, changeDuty } from '@/api/duty.js'
import { formatDate } from '@/utils/index.js'
export default {
  name: 'OperationDuty',
  data() {
    return {
      tableData: [], // 表格数据
      formDate: {
        dutyPerson: '', // 值班人
        dutyDate: '', // 值班信息
        shift: '', // 上班情况（1：上班；0：下班）
        currentStatus: '', // 在岗情况（0：在岗；1：不在岗）
        remarks: '', // 备注信息
        lastModifiedTime: '' // 修改时间
      },
      searchData: {
        dutyPerson: '',
        shift: ''
      },
      rules: {
        dutyPerson: [{ required: true, message: '请输入值班人', trigger: 'blur' }],
        dutyDate: [{ required: true, message: '请选择值班日期', trigger: 'blur' }],
        shift: [{ required: true, message: '请选择上班情况', trigger: 'blur' }],
        currentStatus: [{ required: true, message: '请选择在岗情况', trigger: 'blur' }]
      },
      searchRules: {
        checkAtLeastOne: [
          { validator: this.checkAtLeastOne, trigger: 'blur' }
        ]
      },
      dialogVisible: false, // 弹层是否开启
      isEdit: true, // 是否是编辑
      loading: true,
      total: 0,
      size: 10,
      page: 1
    }
  },
  mounted() {
    this.getDutyData()
  },
  methods: {
    formatDate,
    checkAtLeastOne(rule, value, callback) {
      const { hostName, state } = this.searchData
      if (!hostName && state === null) {
        callback(new Error('至少一个字段不能为空'))
      } else {
        callback()
      }
    },
    commonHandle(res) {
      this.tableData = res.records
      this.total = res.total
      this.size = res.size
      this.loading = false
    },
    // 获取表格数据
    async getDutyData() {
      if (this.searchData.dutyPerson === '' && this.searchData.shift === '') {
        const res = await getDuty(this.page, this.size)
        console.log('值班信息[] ', res)
        this.commonHandle(res)
      } else if (this.searchData.dutyPerson === '') {
        const reqData = {
          shift: this.searchData.shift
        }
        const res = await getDuty(this.page, this.size, reqData)
        this.commonHandle(res)
      } else if (this.searchData.shift === '') {
        const reqData = {
          dutyPerson: this.searchData.dutyPerson
        }
        const res = await getDuty(this.page, this.size, reqData)
        this.commonHandle(res)
      } else {
        const res = await getDuty(this.page, this.size, this.searchData)
        this.commonHandle(res)
      }
    },
    // 新增
    addDuty() {
      this.formDate = {
        dutyPerson: '',
        dutyDate: '',
        shift: '',
        currentStatus: '',
        remarks: ''
      }
      this.dialogVisible = true
      this.isEdit = false
    },
    // 编辑按钮
    handleEdit(row) {
      console.log('id', row.id)
      this.dialogVisible = true
      this.isEdit = true
      // this.formDate = row
      this.formDate = {
        id: row.id,
        dutyPerson: row.dutyPerson,
        dutyDate: row.dutyDate,
        shift: Number(row.shift),
        currentStatus: Number(row.currentStatus),
        remarks: row.remarks,
        lastModifiedTime: row.lastModifiedTime
      }
      // console.log(this.formDate)
    },
    // 删除确认
    handleDelete(row) {
      // console.log(row)
      const id = row.id
      console.log('id:', id)
      this.$confirm('此操作将删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delDuty(id)
        await this.getDutyData()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 弹层 取消
    cancelBtn() {
      this.dialogVisible = false
    },
    // 弹层 确认
    async verBtn() {
      // 编辑-确认
      if (this.isEdit) {
        // console.log(this.formDate)
        // 编辑接口
        this.$refs.formDateRef.validate(async isok => {
          if (isok) {
            await changeDuty(this.formDate)
            this.dialogVisible = false
            // console.log(res)
            this.getDutyData()
            this.$message({
              message: '编辑成功！',
              type: 'success'
            })
          }
        })

        // 添加-确认
      } else {
        // console.log(this.formDate)
        // 添加接口
        this.$refs.formDateRef.validate(async isok => {
          if (isok) {
            await addDuty(this.formDate)
            await this.getDutyData()
            this.dialogVisible = false
            this.$message({
              message: '添加成功！',
              type: 'success'
            })
          }
        })
      }
    },
    // 分页-每页条数
    handleSizeChange(size) {
      this.loading = true
      this.size = size
      this.page = 1
      this.getDutyData()
    },
    // 分页-页码
    handleCurrentChange(page) {
      this.loading = true
      this.page = page
      this.getDutyData()
    },
    async handleSearch() {
      this.$refs.searchForm.validate(async(valid) => {
        if (valid) {
          this.loading = true
          await this.getDutyData()
        }
      })
    },
    async handleRefresh() {
      this.loading = true
      this.searchData.dutyPerson = ''
      this.searchData.shift = ''
      await this.getDutyData()
      this.loading = false
      this.size = 10
      this.page = 1
    }
  }
}
</script>

<style lang="scss" scoped></style>
